// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   MainWindow.java

package presenter;

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.io.PrintStream;
import javax.swing.*;

// Referenced classes of package presenter:
//            SimpleVariableFile, SongFile, SongChooser, Scheduler, 
//            SongDisplayerFrame, TextOutput, Bible, ScreenDetector, 
//            Preferences, Finder, AdvancedSchedulerDialog, SongEditor, 
//            AdvancedScheduler, VideoFrame

public class MainWindow extends JFrame
    implements WindowListener, ActionListener, KeyListener, MouseListener
{

    public MainWindow()
    {
        this(800, 600);
    }

    public MainWindow(int width, int height)
    {
        this(width, height, "./MediaData.txt");
    }

    public MainWindow(int width, int height, String media)
    {
        this(width, height, media, "./FramePositions.txt");
    }

    public MainWindow(int width, int height, String media, String framepos)
    {
        source = true;
        this.width = width;
        this.height = height;
        addWindowListener(this);
        jdp = new JPanel(new BorderLayout());
        split = new JPanel(new GridLayout(2, 1));
        File songDB = new File(media);
        if(!songDB.isFile())
        {
            JFileChooser chooser = new JFileChooser();
            int returnVal = chooser.showOpenDialog(null);
            if(returnVal == 0)
                songDB = chooser.getSelectedFile();
        }
        songs = new SimpleVariableFile(songDB);
        File f = new File("Songs");
        background = new File(songs.getVariable("Background"));
        defaultSong = new SongFile("Default");
        defaultSong.setBackground(songs.getVariable("Background"));
        String colors = songs.getVariable("FontColor");
        int red = Integer.parseInt(colors.substring(1, colors.indexOf('G')));
        int green = Integer.parseInt(colors.substring(colors.indexOf('G') + 1, colors.indexOf('B')));
        int blue = Integer.parseInt(colors.substring(colors.indexOf('B') + 1, colors.length()));
        Color color = new Color(red, green, blue);
        defaultSong.setColor(color);
        sc = new SongChooser(f);
        scheduler = new Scheduler(null, sc.getSongs());
        sd0 = new SongDisplayerFrame(null);
        sd1 = new SongDisplayerFrame(null);
        out = new TextOutput();
        out.setDefaultSong(defaultSong);
        initComponents(framepos);
        try
        {
            b = new Bible(new File((new StringBuilder()).append("Bibles/").append(songs.getVariable("DefaultBible")).toString()));
        }
        catch(Exception e) { }
        Component components[] = {
            sc, scheduler, sd0, sd1
        };
        this.components = components;
        t = new Timer(40, this);
        t.start();
        split.add(scheduler);
        split.add(sc);
        jdp.add(split, "West");
        split1 = new JPanel(new GridLayout(1, 2));
        split1.add(sd0);
        split1.add(sd1);
        jdp.add(split1, "Center");
        jdp.setSize(width, height);
        jdp.setVisible(true);
        menuBar = new JMenuBar();
        fileMenu = new JMenu("File");
        open = new JMenuItem("Open Schedule");
        open.setAccelerator(KeyStroke.getKeyStroke(79, 2));
        save = new JMenuItem("Save Schedule");
        save.setAccelerator(KeyStroke.getKeyStroke(83, 2));
        close = new JMenuItem("Close");
        save.addActionListener(this);
        open.addActionListener(this);
        close.addActionListener(this);
        advancedSchedule = new JMenuItem("Edit Schedule");
        advancedSchedule.setAccelerator(KeyStroke.getKeyStroke(65, 2));
        fileMenu.add(advancedSchedule);
        advancedSchedule.addActionListener(this);
        menuBar.add(fileMenu);
        song = new JMenu("Song");
        newSong = new JMenuItem("New Song");
        newSong.addActionListener(this);
        newSong.setAccelerator(KeyStroke.getKeyStroke(78, 2));
        editSong = new JMenuItem("Edit Song");
        editSong.addActionListener(this);
        editSong.setAccelerator(KeyStroke.getKeyStroke(69, 2));
        findSong = new JMenuItem("Find Song");
        findSong.addActionListener(this);
        findSong.setAccelerator(KeyStroke.getKeyStroke(70, 2));
        editDefault = new JMenuItem("Edit Output Defaults");
        editDefault.addActionListener(this);
        editDefault.setAccelerator(KeyStroke.getKeyStroke(68, 2));
        song.add(newSong);
        song.add(editSong);
        song.add(findSong);
        song.add(editDefault);
        menuBar.add(song);
        options = new JMenu("Options");
        prefs = new JMenuItem("Preferences");
        prefs.addActionListener(this);
        prefs.setAccelerator(KeyStroke.getKeyStroke(80, 2));
        options.add(prefs);
        menuBar.add(options);
        clear = new JToggleButton("Clear", null);
        black = new JToggleButton("Black", null);
        menuBar.add(Box.createHorizontalGlue());
        menuBar.add(clear);
        clear.addActionListener(this);
        menuBar.add(black);
        black.addActionListener(this);
        fileMenu.add(open);
        fileMenu.add(save);
        fileMenu.add(close);
        setJMenuBar(menuBar);
        getContentPane().add(jdp);
        setDefaultCloseOperation(2);
        setVisible(true);
        try
        {
            javax.swing.UIManager.LookAndFeelInfo looks[] = UIManager.getInstalledLookAndFeels();
            for(int i = 0; i < looks.length; i++);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        requestFocus();
    }

    public void updatePositions()
    {
        if(fp == null)
        {
            return;
        } else
        {
            fp.update("OutputX", (new StringBuilder()).append("").append(out.getX()).toString());
            fp.update("OutputY", (new StringBuilder()).append("").append(out.getY()).toString());
            fp.update("OutputWidth", (new StringBuilder()).append("").append(out.getWidth()).toString());
            fp.update("OutputHeight", (new StringBuilder()).append("").append(out.getHeight()).toString());
            fp.write();
            return;
        }
    }

    public void setSize(String f)
    {
        SimpleVariableFile svf = new SimpleVariableFile(new File(f));
        if(svf.exists("MainWindowWidth") && svf.exists("MainWindowHeight"))
        {
            int width = Integer.parseInt(svf.getVariable("MainWindowWidth"));
            int height = Integer.parseInt(svf.getVariable("MainWindowHeight"));
            setSize(width, height);
        }
    }

    public void setIcon(String path)
    {
        setIconImage(Toolkit.getDefaultToolkit().getImage(path));
    }

    public void initComponents(String framepos)
    {
        sc.setVisible(true);
        scheduler.setVisible(true);
        File pos = new File(framepos);
        if(!pos.isFile())
        {
            try
            {
                pos = new File("MonitorData.txt");
                ScreenDetector sd = new ScreenDetector();
                if(pos.isFile())
                {
                    SimpleVariableFile positions = new SimpleVariableFile(pos);
                    //System.out.println((new StringBuilder()).append(positions.getVariable("MainMonitor")).append(" ").append(positions.exists("MainMonitor")).toString());
                    if(positions.exists("MainMonitor") && positions.getVariable("MainMonitor").equals("1"))
                    {
                        //System.out.println("setting to secondary monitor");
                        setBounds(sd.getSecondDisplayBounds());
                    } else
                    {
                        setBounds(sd.getFirstDisplayBounds());
                    }
                    if(positions.exists("OutputMonitor") && positions.getVariable("OutputMonitor").equals("1"))
                    {
                        out.setLocation(0, 0);
                        out.changeSize((int)sd.getSecondDisplayBounds().getWidth(), (int)sd.getSecondDisplayBounds().getHeight());
                    } else
                    {
                        //System.out.println((new StringBuilder()).append("setting output to first monitor ").append(sd.getFirstDisplayBounds()).toString());
                        out.getFrame().setBounds(sd.getFirstDisplayBounds());
                        out.changeSize((int)sd.getFirstDisplayBounds().getWidth(), (int)sd.getFirstDisplayBounds().getHeight());
                    }
                } else
                if(sd.getSecondDisplayBounds() != null)
                {
                    out.getFrame().setBounds(sd.getSecondDisplayBounds());
                    out.changeSize((int)sd.getSecondDisplayBounds().getWidth(), (int)sd.getSecondDisplayBounds().getHeight());
                    setBounds(sd.getFirstDisplayBounds());
                    //System.out.println((new StringBuilder()).append("second screen detected ").append(sd.getSecondDisplayBounds()).toString());
                } else
                {
                    out.changeLocation(0, 0);
                    out.changeSize(800, 600);
                    setBounds(sd.getFirstDisplayBounds());
                }
                out.getFrame().setVisible(true);
            }
            catch(Exception e)
            {
                e.printStackTrace();
            }
            return;
        } else
        {
            fp = new SimpleVariableFile(pos);
            int x = Integer.parseInt(fp.getVariable("OutputX"));
            int y = Integer.parseInt(fp.getVariable("OutputY"));
            int width = Integer.parseInt(fp.getVariable("OutputWidth"));
            int height = Integer.parseInt(fp.getVariable("OutputHeight"));
            out.changeLocation(x, y);
            out.changeSize(width, height);
            out.setVisible(true);
            return;
        }
    }

    public void setSchedule(File f)
    {
        scheduler.setSchedule(f, sc.getSongs(), b);
    }

    public void windowOpened(WindowEvent windowevent)
    {
    }

    public void windowClosing(WindowEvent e)
    {
        if(preferences != null && e.getSource() == preferences)
        {
            out.changeLocation(preferences.getX(), preferences.getY());
            out.changeSize(preferences.getOutputWidth(), preferences.getOutputHeight());
        }
        if(editor != null && e.getSource() == editor.getFrame())
        {
            if(source)
            {
                if(sd0.getSong().getNumber() == editor.getSong().getNumber())
                {
                    sd0.setActive(0);
                    sd0.setSong(editor.getSong());
                }
            } else
            if(sd1.getSong().getNumber() == editor.getSong().getNumber())
            {
                sd1.setActive(0);
                sd1.setSong(editor.getSong());
            }
        } else
        if(defaultEditor != null && e.getSource() == defaultEditor)
        {
            out.setDefaultSong(defaultSong);
            songs.update("Background", defaultSong.getBackground());
            String fontColor = (new StringBuilder()).append("R").append(defaultSong.getColor().getRed()).toString();
            fontColor = (new StringBuilder()).append(fontColor).append("G").append(defaultSong.getColor().getGreen()).toString();
            fontColor = (new StringBuilder()).append(fontColor).append("B").append(defaultSong.getColor().getBlue()).toString();
            songs.update("FontColor", fontColor);
            songs.write();
        }
    }

    public void windowClosed(WindowEvent e)
    {
        if(e.getSource() == this)
            //System.exit(0);
    }

    public void windowIconified(WindowEvent windowevent)
    {
    }

    public void windowDeiconified(WindowEvent windowevent)
    {
    }

    public void windowActivated(WindowEvent windowevent)
    {
    }

    public void windowDeactivated(WindowEvent windowevent)
    {
    }

    public void actionPerformed(ActionEvent event)
    {
        if(event.getSource() == prefs)
        {
            preferences = new Preferences(out.getFrame().getX(), out.getFrame().getY(), out.getFrame().getWidth(), out.getFrame().getHeight());
            preferences.getOk().addActionListener(this);
            preferences.getCancel().addActionListener(this);
        }
        if(preferences != null && event.getSource() == preferences.getOk())
        {
            out.changeLocation(preferences.getOutputX(), preferences.getOutputY());
            out.changeSize(preferences.getOutputWidth(), preferences.getOutputHeight());
            out.setSize(preferences.getOutputWidth(), preferences.getOutputHeight());
            fp.update("OutputX", (new StringBuilder()).append("").append(preferences.getOutputX()).toString());
            fp.update("OutputY", (new StringBuilder()).append("").append(preferences.getOutputY()).toString());
            fp.update("OutputWidth", (new StringBuilder()).append("").append(preferences.getOutputWidth()).toString());
            fp.update("OutputHeight", (new StringBuilder()).append("").append(preferences.getOutputHeight()).toString());
            fp.write();
            preferences.dispose();
        }
        if(preferences != null && event.getSource() == preferences.getCancel())
            preferences.dispose();
        if(event.getSource() == findSong && (finder == null || !finder.isVisible()))
            finder = new Finder(100, 100, 600, 450, sc.getSongs(), scheduler);
        if(event.getSource() == advancedSchedule)
        {
            as = new AdvancedSchedulerDialog(scheduler, b);
            as.setLocation(100, 100);
        }
        if(event.getSource() == editSong)
        {
            if(sd0.getSong() != null && (sc.hasFocus() || scheduler.hasFocus() || sd0.hasFocus()))
            {
                editor = new SongEditor(sd0.getSong());
                editor.getFrame().addWindowListener(this);
                source = source;
            } else
            if(sd1.getSong() != null)
            {
                editor = new SongEditor(sd1.getSong());
                editor.getFrame().addWindowListener(this);
                source = false;
            }
            editor.setLocation(100, 100);
        }
        if(event.getSource() == this.newSong)
        {
            SongFile newSong = new SongFile();
            editor = new SongEditor(newSong);
            editor.setLocation(100, 100);
        }
        if(event.getSource() == editDefault)
        {
            defaultEditor = new AdvancedScheduler(defaultSong, b);
            defaultEditor.addWindowListener(this);
            defaultEditor.setLocation(100, 100);
        }
        if(event.getSource() == clear)
            out.clear();
        if(event.getSource() == black)
            out.black();
        if(event.getSource() == t)
        {
            if(sc.isSongToAdd())
            {
                SongFile sf = sc.getSongAdding();
                scheduler.addSong(sf);
            }
            if(sc.isUpdated() && sc.getSelected() != null && sc.getSelected().getNumber() != sd0.getActive())
            {
                sd0.setSong(sc.getSelected());
                if(vf0 != null)
                    vf0.dispose();
            } else
            if(scheduler.isUpdated() && scheduler.getActive() != null && scheduler.getActive() != sd0.getSong())
            {
                sd0.setSong(scheduler.getActive());
                if(vf0 != null)
                    vf0.dispose();
                if(scheduler.getActive().hasVideo())
                {
                    vf0 = new VideoFrame(out.getWidth(), out.getHeight());
                    vf0.loadVideo(scheduler.getActive().getVideo());
                    vf0.setLocation(100, 100);
                }
            }
            if(sd0.getUpdate())
            {
                sd1.setSong(sd0.getSong());
                sd1.setActive(sd0.getActive());
                if(vf0 != null)
                {
                    if(vf1 != null)
                        vf1.dispose();
                    vf1 = vf0;
                    vf1.setLocation(200, 100);
                    vf0 = null;
                }
                if(sd0.getSong() == scheduler.getActive())
                {
                    scheduler.nextSong();
                    sd0.setSong(scheduler.getActive());
                }
            }
            if(sd1.getUpdate())
            {
                out.setClear(false);
                out.setLive(true);
                black.setSelected(false);
                clear.setSelected(false);
                if(sd1.getSong().getNumber() == -1)
                    out.setText(" ");
                out.setText(sd1.getSong().getVerse(sd1.getActive()));
                out.setForeground(sd1.getSong().getColor());
                if(sd1.getSong().hasVideo())
                    sd1.setSong(sd1.getSong());
            } else
            if(out.getLive() && !out.isClear() && out.isVisible() && sd1.getSong().getNumber() != out.getID())
            {
                String x[] = sd1.getSong().getVerse(sd1.getActive());
                if(sd1.getSong().hasVideo() && vf1 != null)
                    out.setBackground(vf1.getFrame());
                out.setSong(sd1.getSong());
                out.setText(x);
                out.setVerse(sd1.getActive());
            }
            if(sd1.getSong() != null && sd1.getSong().getNumber() == out.getID() && !out.isClear())
            {
                if(sd1.getSong().hasVideo() && vf1 != null)
                    out.setBackground(vf1.getFrame());
                if(sd1.getActive() != out.getVerse() && out.isVisible())
                {
                    String x[] = sd1.getSong().getVerse(sd1.getActive());
                    out.setText(x);
                    out.setVerse(sd1.getActive());
                }
            }
        }
        if(event.getSource() == open)
            scheduler.openSchedule();
        else
        if(event.getSource() == save)
            scheduler.saveSchedule();
        else
        if(event.getSource() == close)
            dispose();
    }

    public void setUndecorated(boolean b)
    {
        out.getFrame().setUndecorated(b);
    }

    public void setCapitalized(boolean b)
    {
        out.setCapitalized(b);
    }

    public static void main(String args[])
    {
        MainWindow mw = null;
        if(args != null && args.length > 0)
        {
            if(args[0].equals("-directory"))
            {
                String dir = (new StringBuilder()).append(args[1]).append("/").toString();
                mw = new MainWindow(800, 600, (new StringBuilder()).append(dir).append("MediaData.txt").toString(), (new StringBuilder()).append(dir).append("FramePositions.txt").toString());
            } else
            {
                for(int i = 0; i < args.length; i++)
                {
                    if(!args[i].equals("-media"))
                        continue;
                    for(int j = i; j < args.length; j++)
                        if(args[j].equals("-positions"))
                            mw = new MainWindow(800, 600, args[i + 1], args[j + 1]);

                }

            }
            if(mw == null)
                mw = new MainWindow();
            for(int i = 0; i < args.length; i++)
            {
                if(args[i].equals("-resolution"))
                    mw.setSize(Integer.parseInt(args[i + 1]), Integer.parseInt(args[i + 2]));
                if(args[i].equals("-schedule"))
                    mw.setSchedule(new File(args[i + 1]));
                if(args[i].equals("-decorate"))
                    mw.setUndecorated(false);
                if(args[i].equals("-capitalize"))
                    mw.setCapitalized(true);
            }

        } else
        {
            mw = new MainWindow();
        }
    }

    public void keyTyped(KeyEvent keyevent)
    {
    }

    public void keyPressed(KeyEvent keyevent)
    {
    }

    public void keyReleased(KeyEvent keyevent)
    {
    }

    public void mouseClicked(MouseEvent mouseevent)
    {
    }

    public void mousePressed(MouseEvent e)
    {
        requestFocus();
    }

    public void mouseReleased(MouseEvent mouseevent)
    {
    }

    public void mouseEntered(MouseEvent mouseevent)
    {
    }

    public void mouseExited(MouseEvent mouseevent)
    {
    }

    private VideoFrame vf0;
    private VideoFrame vf1;
    private Bible b;
    private JPanel jdp;
    private JPanel split;
    private SongChooser sc;
    private Scheduler scheduler;
    private SongDisplayerFrame sd0;
    private SongDisplayerFrame sd1;
    private SimpleVariableFile fp;
    private SimpleVariableFile songs;
    private Component components[];
    private TextOutput out;
    private int width;
    private int height;
    private JToggleButton clear;
    private JToggleButton black;
    private Finder finder;
    private AdvancedSchedulerDialog as;
    private SongEditor editor;
    private boolean source;
    private AdvancedScheduler defaultEditor;
    private Preferences preferences;
    private JMenu options;
    private JMenuItem prefs;
    File background;
    Timer t;
    private Component videoControls;
    SongFile defaultSong;
    JMenuBar menuBar;
    JMenu fileMenu;
    JMenuItem open;
    JMenuItem save;
    JMenuItem close;
    JPanel split1;
    JMenuItem advancedSchedule;
    JMenu song;
    JMenuItem newSong;
    JMenuItem editSong;
    JMenuItem editDefault;
    JMenuItem findSong;
}
